

#include <stdint.h>
#include "reg.h"
#include "sys_settings.h"



void systick_init_with_it(uint32_t count){
	
	uint32_t  tmp;
	if(count  >   ((0x1 << 25) -1)   ){
		tmp =  (0x1 << 25) -1; //0xffffff
	}
	STK_LOAD_REG =  (40*1000*1000)/(4*1000);  //1ms;
	STK_CTRL_REG  |=   1 << 0; //enable
	STK_CTRL_REG  |=   1 << 1; //enable intterrupt
	STK_CTRL_REG  |=   1 << 2; //use core clk source
    	my_fputs("systick_init done\r\n");
}



void  systick_start_delay_ns(){
	STK_LOAD_REG  = 40-1; //reload set 40 clks till down 0;
	STK_CTRL_REG  =0 ;//disable  int
	STK_VAL_REG =0 ; 
	STK_CTRL_REG  |=   (1 << 1 ) | (1 << 2);
	while(STK_VAL_REG !=0);
}

void systick_delay_ns(uint32_t count){
	while(count--)
		systick_start_delay_ns();
}
void systick_delay_ms(uint32_t count){
	while(count--)
		systick_delay_ns(1000);
}

void SysTick_Handler(void){
   my_fputs("sys_tick handlered\r\n");
}

